/*
 * Copyright(C) 2012 Luvina Software Company
 *
 * CommonUtil.java, Jun 1, 2012 HaiPD
 */
package net.luvina.manageuser.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.model.SelectItem;

/**
 * CommonUtil
 *
 * @author HaiPD
 *
 */
public class CommonUtil {

	public static final int FROM_YEAR = 1990;
	public static final int TOTAL_DAY_IN_MONTH = 31;
	public static final int TOTAL_MONTH_IN_YEAR = 12;

	/**
	 * method get limit
	 *
	 * @return limit
	 */
	public static int getLimit() {
		return Integer.parseInt(ConfigProperties.getData("limit"));
	}

	/**
	 * method get total page
	 *
	 * @param totalRecord
	 *            totalRecord
	 * @param limit
	 *            limit
	 * @return total page
	 */
	public static int getTotalPage(int totalRecord, int limit) {
		limit = getLimit();
		return (int) Math.ceil((double) totalRecord / limit);
	}

	/**
	 * method get offset
	 *
	 * @param currentPage
	 *            currentPage
	 * @param limit
	 *            limit
	 * @return offset
	 */
	public static int getOffset(int currentPage, int limit) {
		return (currentPage - 1) * limit;
	}

	/**
	 * method currentSection
	 *
	 * @param currentPage
	 *            currentPage
	 * @return current section
	 */
	public static int getCurrentSection(int currentPage) {
		int pageSection = Integer.parseInt(ConfigProperties
				.getData("pageSection"));
		return (int) Math.ceil((double) currentPage / pageSection);
	}

	/**
	 * method getListPaging
	 *
	 * @param currentPage
	 *            currentPage
	 * @param totalRecord
	 *            totalRecord
	 * @param currentSection
	 *            currentSection
	 * @return list paging
	 */
	public static List<String> getListPaging(int currentPage, int totalRecord,
			int currentSection) {
		List<String> lsPaging = new ArrayList<String>();
		int pageSection = Integer.parseInt(ConfigProperties
				.getData("pageSection"));
		int totalPage = getTotalPage(totalRecord, getLimit());
		int totalSection = (int) Math.ceil((double) totalPage / pageSection);
		int minSection = (currentSection - 1) * pageSection + 1;
		int maxSection = minSection + pageSection - 1;

		if (currentSection > 1) {
			lsPaging.add("<<");
		}

		if (maxSection > totalPage) {
			maxSection = totalPage;
		}

		for (int i = minSection; i <= maxSection; i++) {
			String str = String.valueOf(i);
			lsPaging.add(" " + str + " ");
		}

		if (currentSection < totalSection) {
			lsPaging.add(">>");
		}

		return lsPaging;
	}

	/**
	 * get year now
	 *
	 * @return int year now
	 */
	@SuppressWarnings("deprecation")
	public static int getYearNow() {
		Date now = new Date();
		return now.getYear() + 1900;
	}

	/**
	 * get list year
	 *
	 * @param toYear
	 *            to year
	 * @return list year
	 */
	public static List<SelectItem> getListSelectItemYear(int toYear) {
		SelectItem selectItemYear;
		List<SelectItem> lsSelectItemYear = new ArrayList<SelectItem>();
		for (int i = getYearNow() + toYear; i >= FROM_YEAR; i--) {
			selectItemYear = new SelectItem(i);
			lsSelectItemYear.add(selectItemYear);
		}
		return lsSelectItemYear;
	}

	/**
	 * get list select item month
	 *
	 * @return list select item month
	 */
	public static List<SelectItem> getListSelectItemMonth() {
		SelectItem selectItemMonth;
		List<SelectItem> lsSelectItemMonth = new ArrayList<SelectItem>();
		for (int i = TOTAL_MONTH_IN_YEAR; i >= 1; i--) {
			selectItemMonth = new SelectItem(i);
			lsSelectItemMonth.add(selectItemMonth);
		}
		return lsSelectItemMonth;
	}

	/**
	 * get list select item day
	 *
	 * @return list select item day
	 */
	public static List<SelectItem> getListSelectItemDay() {
		SelectItem selectItemDay;
		List<SelectItem> lsSelectItemDay = new ArrayList<SelectItem>();
		for (int i = TOTAL_DAY_IN_MONTH; i >= 1; i--) {
			selectItemDay = new SelectItem(i);
			lsSelectItemDay.add(selectItemDay);
		}
		return lsSelectItemDay;
	}

	/**
	 * convert to string
	 *
	 * @param year
	 *            year
	 * @param month
	 *            month
	 * @param day
	 *            day
	 * @return string date time
	 */
	public static String convertToString(int year, int month, int day) {
		return year + "/" + month + "/" + day;
	}

	/**
	 * convert date time
	 *
	 * @param year
	 *            year
	 * @param month
	 *            month
	 * @param day
	 *            day
	 * @return date format yyyy/MM/dd
	 */
	public static Date toDate(int year, int month, int day) {
		String str_date = convertToString(year, month, day);

		Date date = new Date();
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		try {
			date = dateFormat.parse(str_date);
		} catch (ParseException e) {
			return null;
		}
		return date;
	}
}
